package com.example.laishichang.sqlite;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import android.database.sqlite.SQLiteDatabase;
import android.content.ContentValues;
import android.widget.Toast;
import android.database.Cursor;

import android.util.Log;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {


    private ASDatabaseHelper dbHelper;
    String key = new String();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dbHelper = new ASDatabaseHelper(this, "AppShare.db", null, 1);

        //  创建数据表
        Button button = (Button)findViewById(R.id.button_CreateTable);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbHelper.getWritableDatabase();
            }
        });


        //  添加一条数据
        Button addDataButton = (Button)findViewById(R.id.button_addData);
        addDataButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dbHelper.insertReportData("hello, world.....1111");
                dbHelper.insertReportData("hello, world.....2222");
                dbHelper.insertReportData("hello, world.....3333");

                Toast.makeText(getApplicationContext(), "insert succeeded", Toast.LENGTH_SHORT).show();
            }
        });


        //  删除第一条缓存数据
        Button button_deleteData = (Button)findViewById(R.id.button_deleteData);
        button_deleteData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                HashMap hashMap = dbHelper.queryFirstReportData();
                if (hashMap.size() > 0) {
                    //  获取当前第一条信息的 id
                    key = hashMap.get("id").toString();
                    //  删除
                    dbHelper.deleteFirstReportData(key);
                    Toast.makeText(getApplicationContext(), "delete succeeded", Toast.LENGTH_SHORT).show();
                } else  {
                    Toast.makeText(getApplicationContext(), "database is empty", Toast.LENGTH_SHORT).show();
                }

            }
        });


        //  查询数据库中第一条数据的id
        Button readDatabaseButton = (Button)findViewById(R.id.button_readDatabase);
        readDatabaseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap hashMap = dbHelper.queryFirstReportData();
                if (hashMap.size()!=0) {
                    key = hashMap.get("id").toString();
                    System.out.println("FirstReportCache is " + hashMap.toString());
                    Toast.makeText(getApplicationContext(), "query succeeded=======" + key, Toast.LENGTH_SHORT).show();
                } else  {
                    Toast.makeText(getApplicationContext(), "query Fail======= 无缓存信息", Toast.LENGTH_SHORT).show();
                }

            }
        });




        //  查询数据库缓存的信息数量
        Button cacheCountButton = (Button)findViewById(R.id.button_CacheCount);
        cacheCountButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = dbHelper.getCacheDataCount();
                Toast.makeText(getApplicationContext(), "缓存数量  =======  " + count, Toast.LENGTH_SHORT).show();

            }
        });
    }





}
